package com.urbo.android.shortcutter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class ImageAdapter extends SimpleAdapter {

	private List<ImageAdapter.IconItemData> listOfItemsIcon;
	
	public ImageAdapter(Context context, List<? extends Map<String, ?>> data, List<ImageAdapter.IconItemData> listOfItemsIcons,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		this.listOfItemsIcon = listOfItemsIcons;
	}

	// List of Drawables to speedup listView browsing. IconItemData contains activityName to sort this list to the same order as listOfItems
	static class IconItemData {
		String label;
		Drawable icon;
	}

	static class ViewHolder {
		ImageView icon;
	}
	
	 @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         
         ViewHolder holder;
      	 convertView = super.getView(position, convertView, parent);   
         if (convertView.getTag() == null)
         {
        	 holder = new ViewHolder();
        	 holder.icon = (ImageView) convertView.findViewById(R.id.row_icon); 
        	 convertView.setTag( holder );
        	 
         } else {
        	 holder = (ViewHolder) convertView.getTag();
         }

		 holder.icon.setImageDrawable(listOfItemsIcon.get( position ).icon);
         return convertView;
     }

}
